using System;
using System.Collections;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.WpfApplication
{
	public class WindsorContainerAbstraction : IIoCContainerAbstraction
	{
		private readonly IWindsorContainer container;

		public WindsorContainerAbstraction()
		{
#if DEBUG
			const string configFile = @"config\IoC-debug.config";
#else
			const string configFile = @"config\IoC.config";
#endif

			this.container = new WindsorContainer(new XmlInterpreter(configFile));
		}

		private WindsorContainerAbstraction(IWindsorContainer container)
		{
			this.container = container;
		}

		public IWindsorContainer Instance
		{
			get { return this.container; }
		}

		#region Implementation of IIoCContainerAbstraction

		public void AddComponentInstance<T>(Type serviceType, object instance)
		{
			Instance.Kernel.AddComponentInstance<T>(serviceType, instance);
		}

		public void ReleaseComponent(object instance)
		{
			Instance.Kernel.ReleaseComponent(instance);
		}

		public T Resolve<T>()
		{
			return this.container.Resolve<T>();
		}

		public T Resolve<T>(IDictionary parameters)
		{
			return this.container.Resolve<T>(parameters);
		}

		public T Resolve<T>(string key)
		{
			return this.container.Resolve<T>(key);
		}

		public T Resolve<T>(string key, IDictionary parameters)
		{
			return this.container.Resolve<T>(key, parameters);
		}

		#endregion
	}
}